package com.pshcp.manager.orderreg;

import com.framework.exception.BizException;
import com.pshcp.common.enums.PayBusinessEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.domain.model.PayInfo;
import com.pshcp.service.OrderRegService;
import com.pshcp.service.PayInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderRegManager {

    @Autowired
    private OrderRegService orderRegService;

    @Autowired
    private PayInfoService payInfoService;

    public void refund(Long orderId) throws BizException {
        OrderReg orderReg = orderRegService.findById(orderId);
        if(orderReg == null){
            return;
        }
        if(RegPayStatusEnum.REFUND_OK.getCode().equals(orderReg.getPayStatus())){
            return;
        }
        OrderReg params = new OrderReg();
        params.setId(orderId);
        params.setPayStatus(RegPayStatusEnum.REFUND_OK.getCode());
        orderRegService.update(params);

        PayInfo payInfo = payInfoService.findByBizIdAndPayBiz(orderId, PayBusinessEnum.REG_PAY.getCode());
        payInfo.setPayStatus(RegPayStatusEnum.REFUND_OK.getCode());
        payInfoService.update(payInfo);
    }


}
